Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VMobject.add_points_as_corners() to return self and safely handle empty points parameter #4091

Merged

Conversation

chopan050
Copy link
Contributor

@chopan050 chopan050 commented Jan 4, 2025

Closes #4090

In the earlier PR #3765, I optimized VMobject.add_points_as_corners() to allocate space for VMobject.points only once, instead of calling N times in a for loop the add_line_to() method which allocated space N times, one for each point. This is really helpful for drawing and animating multiple ParametricFunctions and/or ImplicitFunctions, which tend to require the computation of a lot of points which are then interpolated smoothly.

However, I missed the edge case in which the points parameter contains 0 points, which caused the crash described in #4090. It is possible that the ImplicitFunction algorithm for finding a sample of points belonging to the curve returns a single point for one or more branches (called curves) of the full implicit curve. In that case, the following three lines in ImplicitFunction.generate_points() will fail because of that mistake:

        for curve in curves:
            self.start_new_path(curve[0])
            self.add_points_as_corners(curve[1:])

In this PR, I fix this behavior by returning earlier from VMobject.add_points_as_corners() if points is empty, in a similar way to what the original method already implicitly did (no points = no iterations inside the original for loop).

Plus, instead of returning the passed points (which is useless, because you already had the points beforehand), I made this method return self (which is consistent with many other methods and allows us to chain this method with other subsequent calls).

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test for this?

@chopan050
Copy link
Contributor Author

Could you please add a test for this?

Done!

Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, might want to use np.testing.assert_allclose for better error messages.

@chopan050 chopan050 merged commit 7879fe4 into ManimCommunity:main Jan 5, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when using plot_implicit_curve on Axes with certain range
2 participants